/*
 * ofd.js - A Javascript class for reading and rendering ofd files
 * <https://github.com/DLTech21/ofd.js>
 *
 * Copyright (c) 2020. DLTech21 All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import {calPageBox, renderPage} from "./ofd_render";
import {getPageScal, setMaxPageScal, setPageScal} from "./ofd_util";
import {CreateLibrary, JSBase64ByHashFile, readOFD, onRuntimeInitialized} from "./Fonts";
let pageRenderDic = [];
export const hashOfdDocument = function (options) {
    if (options.ofd instanceof File) {
        var reader = new FileReader();
        // 读取这四个字节
        reader.readAsArrayBuffer(options.ofd);
        reader.onload = function () {
            if (options.success) {
                options.success(JSBase64ByHashFile(reader.result, options.list, options.method))
            }
        }
    } else if (options.ofd instanceof ArrayBuffer) {
        if (options.success) {
            options.success(JSBase64ByHashFile(options.ofd, options.list, options.method))
        }
    }
}

export const parseOfdDocument = function (options) {
    CreateLibrary();
    if (options.ofd instanceof File) {
        var reader = new FileReader();
        // 读取这四个字节
        reader.readAsArrayBuffer(options.ofd);
        reader.onload = function () {
            if (options.success) {
                options.success(readOFD(reader.result))
            }
        }
    } else if (options.ofd instanceof ArrayBuffer) {
        if (options.success) {
            options.success(readOFD(options.ofd))
        }
    }
}

export const renderOfd = function (ofd, documentIndex, pageCount) {
    let divArray = [];
    if (!ofd) {
        return divArray;
    }
    if (!documentIndex) {
        documentIndex = 0
    } else if (documentIndex > ofd.Document.length) {
        documentIndex = ofd.Document.length - 1
    }
    const ofdDocument = ofd.Document[documentIndex]
    pageRenderDic = []
    if (!pageCount || pageCount > ofdDocument.Pages.length) {
        pageCount = ofdDocument.Pages.length
    }
    for (let i = 0; i < ofdDocument.Pages.length; i++) {
        let status = 0;
        const page = ofdDocument.Pages[i];
        let box = calPageBox(ofdDocument, page);
        let pageDiv = document.createElement('div');
        pageDiv.setAttribute('style', `margin-bottom: 20px;position: relative;width:${box.w}px;height:${box.h}px;background: white;overflow: hidden`)
        if (i < pageCount) {
            renderPage(pageDiv, page, ofdDocument);
            status = 2;
        }
        divArray.push(pageDiv);
        pageRenderDic.push(status);
    }
    return divArray;
}

export const renderOfdByIndex = function (pageDiv, ofd, documentIndex, pageIndex) {
    let divArray = [];
    if (!ofd) {
        return divArray;
    }
    if (!documentIndex) {
        documentIndex = 0
    } else if (documentIndex > ofd.Document.length) {
        documentIndex = ofd.Document.length - 1
    }
    const ofdDocument = ofd.Document[documentIndex]
    // if (pageDiv)
    if(pageDiv.childElementCount > 0 && pageRenderDic[pageIndex] === 2) {
        return
    }
    const page = ofdDocument.Pages[pageIndex];
    renderPage(pageDiv, page, ofdDocument);
    pageRenderDic[pageIndex] = 2
}

export const getOFDPageCount = function (ofd, documentIndex) {
    if (!ofd) {
        return 0;
    }
    if (!documentIndex) {
        documentIndex = 0
    } else if (documentIndex > ofd.Document.length) {
        documentIndex = ofd.Document.length - 1
    }
    return ofd.Document[documentIndex].Pages.length
}

export const setPageScale = function (scale) {
    setPageScal(scale);
}

export const getPageScale = function () {
    getPageScal();
}

export const setMaxPageScale = function (maxScale) {
    return setMaxPageScal(maxScale);
}

export const onWebassemblyRuntimeInitialized = function (callback) {
    onRuntimeInitialized(callback)
}

